﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.ApproveFlow.Buz.Models;
using Microhard.ApproveFlow.Buz.Msg;
using Microhard.ApproveFlow.Buz.Interfaces;
using Microhard.Core;
using FluentValidation.Results;

namespace Microhard.ApproveFlow.Buz.Validators
{
    public class FlowChartModelValidator : AbstractValidator<FlowChartModel>
    {
        private IFlowChart iaft = IoC.Resolve<IFlowChart>();

        public FlowChartModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.FlowChartName).NotEmpty().WithMessage(FlowChartModelMsg.NeedName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.TypeId).NotEmpty().WithMessage(FlowChartModelMsg.NeedTypeID).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                Custom(u =>
                {
                    if (iaft.IsFlowChartNameDuplicate(u))
                    {
                        return new ValidationFailure("FlowChartName", FlowChartModelMsg.DuplicateName);
                    }
                    return null;
                });

            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.FlowChartName).NotEmpty().WithMessage(FlowChartModelMsg.NeedName);
                RuleFor(u => u.TypeId).NotEmpty().WithMessage(FlowChartModelMsg.NeedTypeID);

                Custom(u =>
                {
                    if (iaft.IsFlowChartNameDuplicate(u))
                    {
                        return new ValidationFailure("FlowChartName", FlowChartModelMsg.DuplicateName);
                    }
                    return null;
                });

            });
        }
    }
}
